/*!
 * Name: 密码强度验证
 * Version: 1.0.0
 * Modify: By Wang Ping
 * Date: 2013-10-09 15:42:48 (GMT Time)
 */
(function($) {
	$.fn.CipherStrengths = function(options) {
        var defaults = {
			defBg: '#e8ecef', //默认背景颜色
			lowBg: '#F30',    //弱背景颜色
			midBg: '#FC0',    //中背景颜色
			highBg: '#0C3',    //强背景颜色
			curColor: '#fff',    //当前状态文字颜色
			defColor: '#8a949c',  //默认文字颜色
			textSize: '12px',
			width: null //强度提示层宽度
        };
        var options = $.extend(defaults, options);
        this.each(function() {
            new $.CipherStrengths(this, options);
        });
        return this;
    };
    $.CipherStrengths = function(onput, options) {
		var input = $(onput);
		input.attr('autocomplete', 'off');
		$('body').append('<div style="display:none" id="pwdstrength_'+input.attr('id')+'"></div>');
		var $wrap = $("#pwdstrength_"+input.attr('id'));
		var tarWidth = input.outerWidth() - $wrap.outerWidth() + $wrap.width()
		$wrap.css({
			overflow: 'hidden',
			fontSize: options.textSize,
			position: 'absolute',
			top: input.offset().top + input.outerHeight(),
			left: input.offset().left,
			height: 16,
			width: tarWidth,
			zIndex: 9999
		});
		var tempWidth = tarWidth / 3 - 1;
		var BL,BM,BH,TL,TM,TH;
		BL = BM = BH = options.defBg;
		TL = TM = TH = options.defColor;
		input.keyup(function() {
			checkSVal()
        }).blur(function() {
			checkSVal()
        });
		//测试某个字符是属于哪一类
		function charMode(iN) {
			if (iN >= 48 && iN <= 57) //数字  
			return 1;
			if (iN >= 65 && iN <= 90) //大写字母  
			return 2;
			if (iN >= 97 && iN <= 122) //小写  
			return 4;
			else return 8; //特殊字符  
		};
		//计算出当前密码当中一共有多少种模式
		function bitTotal(n) {
			var m = 0;
			for (i = 0; i < 4; i++) {
				if (n & 1) m++;
				n >>>= 1;
			}
			return m;
		};
		//返回密码的强度级别  
		function checkStrong(c) {
			if (c.length <= 5) return 0; //密码太短
			var m = 0;
			for (var i = 0; i < c.length; i++) {
				//测试每一个字符的类别并统计一共有多少种模式
				m |= charMode(c.charCodeAt(i));
			}
			return bitTotal(m);
		};
		function checkSVal(){
			var val = input.val();
			if (val == null || val == '' || val.length <= 5) {
				BL = BM = BH = options.defBg;
				TL = TM = TH = options.defColor;
				$wrap.hide();
			} else {
				var s = checkStrong(val);
				switch (s) {
				case 0:
					BL = BM = BH = options.defBg;
					TL = TM = TH = options.defColor;
					break;
				case 1:
					BL = options.lowBg;
					TL = options.curColor;
					break;
				case 2:
					BL = options.lowBg;
					BM = options.midBg;
					TL = options.curColor;
					TM = options.curColor;
					break;
				case 3:
					BL = options.lowBg;
					BM = options.midBg;
					BH = options.highBg;
					TL = options.curColor;
					TM = options.curColor;
					TH = options.curColor;
					break;
				default:
					break;
				};
				var sty = 'width:'+tempWidth+'px;float:left; margin-right:1px; text-align: center; height:16px; line-height:normal; vertical-align:text-top; overflow:hidden';
				var html = '<span style="'+sty+'; background:'+BL+';color:'+TL+'">弱</span>';
				html += '<span style="'+sty+'; px;background:'+BM+';color:'+TM+'">中</span>';
				html += '<span style="'+sty+'; px;background:'+BH+';color:'+TH+'">强</span>';
				$wrap.html(html).fadeIn();
			};
		};
	}
})(jQuery);